package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/update")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据
        String idString = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        String sessionCode = (String) req.getSession().getAttribute("code");
        String address = req.getParameter("address");
        String email = req.getParameter("email");
        String hobby = req.getParameter("hobby");
        String birthday = req.getParameter("birthday");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");


//        根据拿到的数据去数据库查询是否有当前的用户名和密码
//        首先需要判断验证码是否输入正确,是属于逻辑关系，
//        所以一般情况会传入到service层去做
//        2.封装数据
        UserModel userModel = new UserModel();
        userModel.setId(Tool.stringToInt(idString));
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setCode(code);
        userModel.setSessionCode(sessionCode);

//        3.调用逻辑层
        UserService userService = new UserServiceImpl();
//        4.得到返回数据
        String result = userService.update(userModel);
//所以我们需要将结果封装成json格式的对象
// 5.返回数据到页面
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
}
